package weaver.interfaces.linearin.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.linearin.duss.utils.KingdeeUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecologyidea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 20231206
 * @Description: 差旅费报销审批（先积）流程归档后调用生成凭证接口
 * @ModifyLog:
 **/
public class WeaClfbxlcSYBAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaClfbxlcSYBAction>>>>>requestid=" + requestid);
        try {
            JSONObject dataJson = new JSONObject();
            String message = "调用金蝶系统接口返回:";
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            ResourceComInfo comInfo = new ResourceComInfo();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String sqr = Util.null2String(rs.getString("sqr"));
            String zfrq = Util.null2String(rs.getString("zfrq"));
            String jdrybm = Util.null2String(rs.getString("jdrybm"));
            String jdbmbm = Util.null2String(rs.getString("jdbmbm"));
            String zfzhdxwb = Util.null2String(rs.getString("zfzhdxwb"));
            String lcbh = Util.null2String(rs.getString("lcbh"));
            //明细表1发票金额（dt1.fpje）+明细表2发票金额含税（dt2.fpje）+明细表3金额合计（人民币）(dt3.jehjrmb)+明细表4发票金额(dt4.fpje)
            rs.executeQuery("select sum(a.fpje) fpje,a.szsyb from (select sum(fpje) fpje,szsyb from "+tablename+"_dt1 where mainid='"+mainid+"' and isnull(szsyb,'') !='' group by szsyb \n" +
                    "union all select sum(zsfbhsje) fpje,szsyb from "+tablename+"_dt2 where mainid='"+mainid+"' and isnull(szsyb,'') !='' group by szsyb \n" +
                    "union all select sum(jehjrmb) fpje,szsyb from "+tablename+"_dt3 where mainid='"+mainid+"' and isnull(szsyb,'') !='' group by szsyb \n" +
                    "union all select sum(fpje) fpje,szsyb from "+tablename+"_dt4 where mainid='"+mainid+"' and isnull(szsyb,'') !='' group by szsyb) a group by a.szsyb");
            while (rs.next()) {
                String szsyb = Util.null2String(rs.getString("szsyb"));
                double fpjehj = Util.getDoubleValue(rs.getString("fpje"));
                String bookid = KingdeeUtil.getBookId(szsyb);
                //单据体：FEntity
                JSONArray FEntity = new JSONArray();
                /*单据体第一行*/
                rs1.executeQuery("select yfdl,hjkm,xm,sum(fpjehbs) fpjehbs from " + tablename + "_dt1 where mainid=? and szsyb=? group by yfdl,hjkm,xm", mainid,szsyb);
                while (rs1.next()) {
                    JSONObject FEntityJSON = new JSONObject();
                    String hjkm = Util.null2String(rs1.getString("hjkm"));
                    String yfdl = Util.null2String(rs1.getString("yfdl"));
                    double fpjehbs = Util.getDoubleValue(rs1.getString("fpjehbs"));
                    String xm = Util.null2String(rs1.getString("xm"));
                    if (fpjehbs > 0) {
                        //摘要
                        String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费交通费";
                        FEntityJSON.put("FEXPLANATION", fexplanation);
                        //科目编码
                        JSONObject FACCOUNTID = new JSONObject();
                        FACCOUNTID.put("FNumber", hjkm);
                        FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                        //核算维度
                        JSONObject FDetailID = new JSONObject();
                        //金蝶部门编码
                        if (!"".equals(jdbmbm)) {
                            JSONObject FDETAILID__FFLEX5 = new JSONObject();
                            FDETAILID__FFLEX5.put("FNumber", jdbmbm);
                            FDetailID.put("FDETAILID__FFLEX5", FDETAILID__FFLEX5);
                        }
                        //研发大类
                        if (!"".equals(yfdl)) {
                            JSONObject FDETAILID__FF100002 = new JSONObject();
                            FDETAILID__FF100002.put("FNumber", KingdeeUtil.getYfdlBm(yfdl));
                            FDetailID.put("FDETAILID__FF100002", FDETAILID__FF100002);
                        }
                        //金蝶人员编码
                        if (!"".equals(jdrybm)) {
                            JSONObject FDETAILID__FFLEX7 = new JSONObject();
                            FDETAILID__FFLEX7.put("FNumber", jdrybm);
                            FDetailID.put("FDETAILID__FFLEX7", FDETAILID__FFLEX7);
                        }
                        //金蝶项目编码
                        if (!"".equals(xm)) {
                            JSONObject FDETAILID__FF100019 = new JSONObject();
                            FDETAILID__FF100019.put("FNumber", KingdeeUtil.getXmbh(xm));
                            FDetailID.put("FDETAILID__FF100019", FDETAILID__FF100019);
                        }
                        FEntityJSON.put("FDetailID", FDetailID);
                        //借方金额
                        FEntityJSON.put("FDEBIT", fpjehbs);
                        FEntity.add(FEntityJSON);
                    }
                }
                /*单据体第二行*/
                rs1.executeQuery("select fphm,sum(se) se from " + tablename + "_dt1 where mainid=? and fplx=1 and szsyb=? group by fphm", mainid,szsyb);
                while (rs1.next()) {
                    JSONObject FEntityJSON = new JSONObject();
                    String fphm = Util.null2String(rs1.getString("fphm"));
                    double se = Util.getDoubleValue(rs1.getString("se"));
                    if (se > 0) {
                        //摘要
                        String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费交通费" + fphm;
                        FEntityJSON.put("FEXPLANATION", fexplanation);
                        //科目编码
                        JSONObject FACCOUNTID = new JSONObject();
                        FACCOUNTID.put("FNumber", "2221.01.01.01");
                        FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                        //借方金额
                        FEntityJSON.put("FDEBIT", se);
                        FEntity.add(FEntityJSON);
                    }

                }
                /*单据体第三行*/
                rs1.executeQuery("select yfdl,hjkm,xm,sum(jehjrmb) jehjrmb from " + tablename + "_dt3 where mainid=? and szsyb=? group by yfdl,hjkm,xm", mainid,szsyb);
                while (rs1.next()) {
                    JSONObject FEntityJSON = new JSONObject();
                    String hjkm = Util.null2String(rs1.getString("hjkm"));
                    String yfdl = Util.null2String(rs1.getString("yfdl"));
                    double jehjrmb = Util.getDoubleValue(rs1.getString("jehjrmb"));
                    String xm = Util.null2String(rs1.getString("xm"));
                    if (jehjrmb > 0) {
                        //摘要
                        String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费其他交通费";
                        FEntityJSON.put("FEXPLANATION", fexplanation);
                        //科目编码
                        JSONObject FACCOUNTID = new JSONObject();
                        FACCOUNTID.put("FNumber", hjkm);
                        FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                        //核算维度
                        JSONObject FDetailID = new JSONObject();
                        //金蝶部门编码
                        if (!"".equals(jdbmbm)) {
                            JSONObject FDETAILID__FFLEX5 = new JSONObject();
                            FDETAILID__FFLEX5.put("FNumber", jdbmbm);
                            FDetailID.put("FDETAILID__FFLEX5", FDETAILID__FFLEX5);
                        }
                        //研发大类
                        if (!"".equals(yfdl)) {
                            JSONObject FDETAILID__FF100002 = new JSONObject();
                            FDETAILID__FF100002.put("FNumber", KingdeeUtil.getYfdlBm(yfdl));
                            FDetailID.put("FDETAILID__FF100002", FDETAILID__FF100002);
                        }
                        //金蝶人员编码
                        if (!"".equals(jdrybm)) {
                            JSONObject FDETAILID__FFLEX7 = new JSONObject();
                            FDETAILID__FFLEX7.put("FNumber", jdrybm);
                            FDetailID.put("FDETAILID__FFLEX7", FDETAILID__FFLEX7);
                        }
                        //金蝶项目编码
                        if (!"".equals(xm)) {
                            JSONObject FDETAILID__FF100019 = new JSONObject();
                            FDETAILID__FF100019.put("FNumber", KingdeeUtil.getXmbh(xm));
                            FDetailID.put("FDETAILID__FF100019", FDETAILID__FF100019);
                        }
                        FEntityJSON.put("FDetailID", FDetailID);
                        //借方金额
                        FEntityJSON.put("FDEBIT", jehjrmb);
                        FEntity.add(FEntityJSON);
                    }
                }
                /*单据体第四行*/
                rs1.executeQuery("select yfdl,hjkm,xm,sum(zsfbhsje) zsfbhsje from " + tablename + "_dt2 where mainid=? and szsyb=? group by yfdl,hjkm,xm", mainid,szsyb);
                while (rs1.next()) {
                    JSONObject FEntityJSON = new JSONObject();
                    String hjkm = Util.null2String(rs1.getString("hjkm"));
                    String yfdl = Util.null2String(rs1.getString("yfdl"));
                    double zsfbhsje = Util.getDoubleValue(rs1.getString("zsfbhsje"));
                    String xm = Util.null2String(rs1.getString("xm"));
                    if (zsfbhsje > 0) {
                        //摘要
                        String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费住宿费";
                        FEntityJSON.put("FEXPLANATION", fexplanation);
                        //科目编码
                        JSONObject FACCOUNTID = new JSONObject();
                        FACCOUNTID.put("FNumber", hjkm);
                        FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                        //核算维度
                        JSONObject FDetailID = new JSONObject();
                        //金蝶部门编码
                        if (!"".equals(jdbmbm)) {
                            JSONObject FDETAILID__FFLEX5 = new JSONObject();
                            FDETAILID__FFLEX5.put("FNumber", jdbmbm);
                            FDetailID.put("FDETAILID__FFLEX5", FDETAILID__FFLEX5);
                        }
                        //研发大类
                        if (!"".equals(yfdl)) {
                            JSONObject FDETAILID__FF100002 = new JSONObject();
                            FDETAILID__FF100002.put("FNumber", KingdeeUtil.getYfdlBm(yfdl));
                            FDetailID.put("FDETAILID__FF100002", FDETAILID__FF100002);
                        }
                        //金蝶人员编码
                        if (!"".equals(jdrybm)) {
                            JSONObject FDETAILID__FFLEX7 = new JSONObject();
                            FDETAILID__FFLEX7.put("FNumber", jdrybm);
                            FDetailID.put("FDETAILID__FFLEX7", FDETAILID__FFLEX7);
                        }
                        //金蝶项目编码
                        if (!"".equals(xm)) {
                            JSONObject FDETAILID__FF100019 = new JSONObject();
                            FDETAILID__FF100019.put("FNumber", KingdeeUtil.getXmbh(xm));
                            FDetailID.put("FDETAILID__FF100019", FDETAILID__FF100019);
                        }
                        FEntityJSON.put("FDetailID", FDetailID);
                        //借方金额
                        FEntityJSON.put("FDEBIT", zsfbhsje);
                        FEntity.add(FEntityJSON);
                    }
                }
                /*单据体第五行*/
                rs1.executeQuery("select fphm,sum(fpse) fpse from " + tablename + "_dt2 where mainid=? and fplx=1 and szsyb=? group by fphm", mainid,szsyb);
                while (rs1.next()) {
                    JSONObject FEntityJSON = new JSONObject();
                    String fphm = Util.null2String(rs1.getString("fphm"));
                    double fpse = Util.getDoubleValue(rs1.getString("fpse"));
                    if (fpse > 0) {
                        //摘要
                        String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费住宿费" + fphm;
                        FEntityJSON.put("FEXPLANATION", fexplanation);
                        //科目编码
                        JSONObject FACCOUNTID = new JSONObject();
                        FACCOUNTID.put("FNumber", "2221.01.01.01");
                        FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                        //借方金额
                        FEntityJSON.put("FDEBIT", fpse);
                        FEntity.add(FEntityJSON);
                    }

                }
                /*单据体第六行*/
                rs1.executeQuery("select yfdl,hjkm,xm,sum(fpjebhsje) fpjebhsje from " + tablename + "_dt4 where mainid=? and szsyb=? group by yfdl,hjkm,xm", mainid,szsyb);
                while (rs1.next()) {
                    JSONObject FEntityJSON = new JSONObject();
                    String hjkm = Util.null2String(rs1.getString("hjkm"));
                    String yfdl = Util.null2String(rs1.getString("yfdl"));
                    double fpjebhsje = Util.getDoubleValue(rs1.getString("fpjebhsje"));
                    String xm = Util.null2String(rs1.getString("xm"));
                    if (fpjebhsje > 0) {
                        //摘要
                        String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费其他费用";
                        FEntityJSON.put("FEXPLANATION", fexplanation);
                        //科目编码
                        JSONObject FACCOUNTID = new JSONObject();
                        FACCOUNTID.put("FNumber", hjkm);
                        FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                        //核算维度
                        JSONObject FDetailID = new JSONObject();
                        //金蝶部门编码
                        if (!"".equals(jdbmbm)) {
                            JSONObject FDETAILID__FFLEX5 = new JSONObject();
                            FDETAILID__FFLEX5.put("FNumber", jdbmbm);
                            FDetailID.put("FDETAILID__FFLEX5", FDETAILID__FFLEX5);
                        }
                        //研发大类
                        if (!"".equals(yfdl)) {
                            JSONObject FDETAILID__FF100002 = new JSONObject();
                            FDETAILID__FF100002.put("FNumber", KingdeeUtil.getYfdlBm(yfdl));
                            FDetailID.put("FDETAILID__FF100002", FDETAILID__FF100002);
                        }
                        //金蝶人员编码
                        if (!"".equals(jdrybm)) {
                            JSONObject FDETAILID__FFLEX7 = new JSONObject();
                            FDETAILID__FFLEX7.put("FNumber", jdrybm);
                            FDetailID.put("FDETAILID__FFLEX7", FDETAILID__FFLEX7);
                        }
                        //金蝶项目编码
                        if (!"".equals(xm)) {
                            JSONObject FDETAILID__FF100019 = new JSONObject();
                            FDETAILID__FF100019.put("FNumber", KingdeeUtil.getXmbh(xm));
                            FDetailID.put("FDETAILID__FF100019", FDETAILID__FF100019);
                        }
                        FEntityJSON.put("FDetailID", FDetailID);
                        //借方金额
                        FEntityJSON.put("FDEBIT", fpjebhsje);
                        FEntity.add(FEntityJSON);
                    }
                }
                /*单据体第七行*/
                rs1.executeQuery("select fphm,sum(fpse) fpse from " + tablename + "_dt4 where mainid=? and fplx=1 and szsyb=? group by fphm", mainid,szsyb);
                while (rs1.next()) {
                    JSONObject FEntityJSON = new JSONObject();
                    String fphm = Util.null2String(rs1.getString("fphm"));
                    double fpse = Util.getDoubleValue(rs1.getString("fpse"));
                    if (fpse > 0) {
                        //摘要
                        String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费其他费用" + fphm;
                        FEntityJSON.put("FEXPLANATION", fexplanation);
                        //科目编码
                        JSONObject FACCOUNTID = new JSONObject();
                        FACCOUNTID.put("FNumber", "2221.01.01.01");
                        FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                        //借方金额
                        FEntityJSON.put("FDEBIT", fpse);
                        FEntity.add(FEntityJSON);
                    }

                }
                /*单据体第八行*/
                boolean flag8 = fpjehj > 0;
                if (flag8) {
                    //摘要
                    JSONObject FEntityJSON = new JSONObject();
                    String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费差旅费";
                    FEntityJSON.put("FEXPLANATION", fexplanation);
                    //科目编码
                    JSONObject FACCOUNTID = new JSONObject();
                    FACCOUNTID.put("FNumber", "2241.03");
                    FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                    //核算维度
                    JSONObject FDetailID = new JSONObject();
                    //金蝶人员编码
                    JSONObject FDETAILID__FFLEX7 = new JSONObject();
                    FDETAILID__FFLEX7.put("FNumber", jdrybm);
                    FDetailID.put("FDETAILID__FFLEX7", FDETAILID__FFLEX7);
                    FEntityJSON.put("FDetailID", FDetailID);
                    //贷方金额
                    FEntityJSON.put("FCREDIT", fpjehj);
                    FEntity.add(FEntityJSON);
                }
                /*单据体第九行*/
                boolean flag9 = fpjehj > 0;
                if (flag9) {
                    //摘要
                    JSONObject FEntityJSON = new JSONObject();
                    String fexplanation = lcbh+"支付" + comInfo.getLastname(sqr) + "报销差旅费差旅费";
                    FEntityJSON.put("FEXPLANATION", fexplanation);
                    //科目编码
                    JSONObject FACCOUNTID = new JSONObject();
                    FACCOUNTID.put("FNumber", "2241.03");
                    FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                    //核算维度
                    JSONObject FDetailID = new JSONObject();
                    //金蝶人员编码
                    JSONObject FDETAILID__FFLEX7 = new JSONObject();
                    FDETAILID__FFLEX7.put("FNumber", jdrybm);
                    FDetailID.put("FDETAILID__FFLEX7", FDETAILID__FFLEX7);
                    FEntityJSON.put("FDetailID", FDetailID);
                    //借方金额
                    FEntityJSON.put("FDEBIT", fpjehj);
                    FEntity.add(FEntityJSON);
                }

                /*单据体第十行*/
                boolean flag10 = fpjehj > 0;
                if (flag10) {
                    //摘要
                    JSONObject FEntityJSON = new JSONObject();
                    String fexplanation = lcbh+"支付" + comInfo.getLastname(sqr) + "报销费用";
                    FEntityJSON.put("FEXPLANATION", fexplanation);
                    //科目编码
                    JSONObject FACCOUNTID = new JSONObject();
                    FACCOUNTID.put("FNumber", "1002.01");
                    FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                    //核算维度
                    JSONObject FDetailID = new JSONObject();
                    //银行账号
                    JSONObject FDETAILID__FF100006 = new JSONObject();
                    FDETAILID__FF100006.put("FNumber", zfzhdxwb);
                    FDetailID.put("FDETAILID__FF100006", FDETAILID__FF100006);
                    FEntityJSON.put("FDetailID", FDetailID);
                    //贷方金额
                    FEntityJSON.put("FCREDIT", fpjehj);
                    FEntity.add(FEntityJSON);
                }
                dataJson.put("zfrq", zfrq);
                dataJson.put("FEntity", FEntity);
                writeLog(">>>>>WeaClfbxlcSYBAction>>>>>dataJson=" + dataJson.toJSONString());
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaClfbxlcSYBAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("bookid", bookid);
                dataMap.put("cz", src);
                JSONObject resultJson = KingdeeUtil.passCertificate(dataJson, dataMap);
                writeLog(">>>>>WeaClfbxlcSYBAction>>>>>resultJson=" + resultJson.toJSONString());
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String dataStr = Util.null2String(resultJson.getString("data"));
                    JSONObject retrunDataJson = JSONObject.parseObject(dataStr);
                    JSONObject ResultJson = JSONObject.parseObject(retrunDataJson.getString("Result"));
                    JSONObject ResponseStatusJson = JSONObject.parseObject(ResultJson.getString("ResponseStatus"));
                    String IsSuccess = Util.null2String(ResponseStatusJson.getString("IsSuccess"));
                    if ("false".equals(IsSuccess)) {
                        message += Util.null2String(ResponseStatusJson.getString("Errors"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}